//
//  RegistViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/25.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "RegistViewC.h"
#import "Util.h"
#import "UserModel.h"
#import <MBProgressHUD.h>

@interface RegistViewC ()

@end

@implementation RegistViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"免费注册";
    
    [_clickBtn setImage:[UIImage imageNamed:@"unclick.png"] forState:UIControlStateNormal];
    [_clickBtn setImage:[UIImage imageNamed:@"click.png"] forState:UIControlStateSelected];
    _passField.secureTextEntry = YES;
    _compField.secureTextEntry = YES;
   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == _nameField) {
        NSString *name = _nameField.text;
        [Util changeNSString:name];
        if (name.length != 11) {
            UIAlertView *alertvc = [[UIAlertView alloc] initWithTitle:@"提示"
                                                              message:@"请正确填写11位手机号码"
                                delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertvc show];
            return ;
        }
        [[UserModel sharedInstance] checkAccountIsExist:name successTele:^(BOOL successed, unsigned int value) {
             MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            if (successed) {
                HUD.labelText = @"用户名可以使用";
                [HUD hide:YES afterDelay:1.5f];
            }
            
            else{
                if (value == 0) {
                    HUD.labelText = @"用户名已存在请重新填写";
                    [HUD hide:YES afterDelay:1.5f];
                }
                
                if (value == 2) {
                    HUD.labelText = @"程序出错";
                    [HUD hide:YES afterDelay:1.5f];
                }
            }
        }];

    }
    
    if (textField == _codeField) {
        NSString *code = _codeField.text;
        NSString *name = _nameField.text;
        [Util changeNSString:code];
        [[UserModel sharedInstance] checkSmsCode:name smscode:code
            successTeleNumber:^(BOOL successed, unsigned int value) {
                 MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                if (successed) {
                    HUD.labelText = @"验证码输入正确";
                    [HUD hide:YES afterDelay:1.5f];
                }
                else{
                    if (value == 1) {
                        HUD.labelText = @"验证码输入错误请重新填写";
                        [HUD hide:YES afterDelay:1.5f];
                    }
                }
      }];
    }
}

- (IBAction)codeBtn:(id)sender{
    
    NSString *tele = _nameField.text;
    [Util changeNSString:tele];
    [[UserModel sharedInstance] getSmsCode:tele
         successTeleNum:^(BOOL successed, unsigned int value) {
    }];
    _codeBtn.enabled = NO;
}

- (IBAction)clickBtn:(id)sender{
    
    _clickBtn.selected = !_clickBtn.selected;
}

- (IBAction)registBtn:(id)sender{
    NSString *name = _nameField.text;
    NSString *password = _passField.text;
    NSString *comp = _compField.text;
    
     MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if (name.length == 0 || password.length == 0 || comp.length == 0) {
        HUD.labelText = @"请将资料填写完整";
        [HUD hide:YES afterDelay:1.5f];
        return ;
    }
    
    if (_clickBtn.imageView.image != [UIImage imageNamed:@"click.png"]) {
        HUD.labelText = @"请阅读并同意软件许可";
        [HUD hide:YES afterDelay:1.5f];
        return ;
    }
    [Util changeNSString:name];
    [Util changeNSString:password];
    
    
    if ([password isEqualToString:comp]) {
        [[UserModel sharedInstance] registWithLoginname:name password:password
    successBlock:^(BOOL successed, unsigned int value) {
        
        if (successed) {
            [self dismissViewControllerAnimated:YES completion:^{
                [[NSNotificationCenter defaultCenter]
                 postNotificationName:@"NOTIFICATION_AUTOLOGIN"
                 object:@{@"loginName": name,
                          @"password": password}];
                }];
        }
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
